<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑结构 -->
      <!--
            面包屑结构：组件套组件，内容用插槽注入
         -->
      <!--
             分隔面包屑菜单的符号：separator='/'
             先传给bread组件
             再传给item组件：作用域插槽，依赖注入 ->这里使用依赖注入
          -->
      <xtx-bread separator="/">
        <!--
                首页等名字用插槽方式注入
             -->
        <xtx-bread-item to="/">首页</xtx-bread-item>
        <xtx-bread-item>{{ categoryList.name }}</xtx-bread-item>
      </xtx-bread>

      <!-- 轮播图 -->
      <xtx-slider :data="sliders"></xtx-slider>

      <!-- 二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="second in categoryList.children" :key="second.id">
            <a href="javascript:;">
              <img :src="second.picture" />
              <p>{{ second.name }}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 分类关联商品 -->
      <div
        class="ref-goods"
        v-for="second in categoryList.children"
        :key="second.id"
      >
        <!-- 二级分类每一项名字 -->
        <div class="head">
          <h3>- {{ second.name }} -</h3>
          <XtxMore to="/" />
        </div>
        <!-- 二级分类每一项具体内容 -->
        <div class="body">
          <!--
                封装二级分类每一项图片为组件
             -->
          <good-item
            v-for="good in second.goods"
            :key="good.id"
            :good="good"
          ></good-item>

          <!--
          <RouterLink
            to="/"
            class="goods-item"
            v-for="good in second.goods"
            :key="good.id"
          >
            <img :src="good.picture" alt="" />
            <p class="name ellipsis">{{ good.name }}</p>
            <p class="desc ellipsis">{{ good.tag }}</p>
            <p class="price">&yen;{{ good.price }}</p>
          </RouterLink>
          -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useBanner } from '@/hooks/page'
import { findAllCategory } from '@/api/category'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'

import GoodItem from './components/GoodItem'

// 获取对应id的分类
const useCategory = () => {
  const route = useRoute()
  const categoryList = ref([])

  const getCategoryList = async () => {
    const { result } = await findAllCategory(route.params.id)
    categoryList.value = result
    // console.log(categoryList.value.children)
  }

  // =========!!!!!得加onMOunted
  onMounted(() => {
    getCategoryList()
  })

  return { categoryList }
}

export default {
  name: 'TopCategory',

  components: {
    GoodItem
  },

  setup () {
    // 轮播图页面业务复用
    const { sliders } = useBanner()

    //  对应id的频道列表
    const { categoryList } = useCategory()

    return { sliders, categoryList }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }

    .goods-item {
      display: block;
      width: 220px;
      padding: 20px 30px;
      text-align: center;
      img {
        width: 160px;
        height: 160px;
      }
      p {
        padding-top: 10px;
      }
      .name {
        font-size: 16px;
      }
      .desc {
        color: #999;
        height: 29px;
      }
      .price {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}
</style>
